using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WinSAG
{
    internal class CommandLineGenerator
    {
        ProjectProperties propertiesObject;
        string tmpFilename = null;

        internal CommandLineGenerator(ProjectProperties pp)
        {
            propertiesObject = pp;
            try { tmpFilename = Path.GetTempFileName(); }
            catch (Exception ) { tmpFilename = @"$$winsag_1234$$.tmp";}
        }

        internal string ResultsFilename 
        {
            get { return tmpFilename; }
        }

        internal string getCommandLine()
        {
            StringBuilder cmdLine = new StringBuilder();

            if (propertiesObject.BalanceCurlyBraces == true)
            {
                cmdLine.Append(" -balanceCurlyBraces+");
            }
            else
            {
                cmdLine.Append(" -balanceCurlyBraces-");
            }

            if (propertiesObject.BalanceParentheses == true)
            {
                cmdLine.Append(" -balanceParentheses+");
            }
            else
            {
                cmdLine.Append(" -balanceParentheses-");
            }

            if (propertiesObject.BalanceSquareBrackets== true)
            {
                cmdLine.Append(" -balanceSquareBrackets+");
            }
            else
            {
                cmdLine.Append(" -balanceSquareBrackets-");
            }

            if (propertiesObject.ExcludeFiles != null)
            {
                cmdLine.Append(" -excludes="+propertiesObject.ExcludeFiles);
            }            
            
            if (propertiesObject.IgnoreBlocks != null)
            {
                cmdLine.Append(" -ignoreBlocks="+propertiesObject.IgnoreBlocks);
            }            

            if (propertiesObject.IgnoreCharacterCase== true)
            {
                cmdLine.Append(" -ignoreCharacterCase+");
            }
            else
            {
                cmdLine.Append(" -ignoreCharacterCase-");
            }

            if (propertiesObject.IgnoreCharacters == true)
            {
                cmdLine.Append(" -ignoreCharacters+");
            }
            else
            {
                cmdLine.Append(" -ignoreCharacters-");
            }

            if (propertiesObject.IgnoreCurlyBraces == true)
            {
                cmdLine.Append(" -ignoreCurlyBraces+");
            }
            else
            {
                cmdLine.Append(" -ignoreCurlyBraces-");
            }

            if (propertiesObject.IgnoreIdentifierCase == true)
            {
                cmdLine.Append(" -ignoreIdentifierCase+");
            }
            else
            {
                cmdLine.Append(" -ignoreIdentifierCase-");
            }

            if (propertiesObject.IgnoreLiterals == true)
            {
                cmdLine.Append(" -ignoreLiterals+");
            }
            else
            {
                cmdLine.Append(" -ignoreLiterals-");
            }

            if (propertiesObject.IgnoreModifiers == true)
            {
                cmdLine.Append(" -ignoreModifiers+");
            }
            else
            {
                cmdLine.Append(" -ignoreModifiers-");
            }

            if (propertiesObject.IgnoreNumbers == true)
            {
                cmdLine.Append(" -ignoreNumbers+");
            }
            else
            {
                cmdLine.Append(" -ignoreNumbers-");
            }
            
            if (propertiesObject.IgnoreRegions == true)
            {
                cmdLine.Append(" -ignoreRegions+");
            }
            else
            {
                cmdLine.Append(" -ignoreRegions-");
            }

            if (propertiesObject.IgnoreStringCase == true)
            {
                cmdLine.Append(" -ignoreStringCase+");
            }
            else
            {
                cmdLine.Append(" -ignoreStringCase-");
            }

            if (propertiesObject.IgnoreStrings == true)
            {
                cmdLine.Append(" -ignoreStrings+");
            }
            else
            {
                cmdLine.Append(" -ignoreStrings-");
            }

            if (propertiesObject.IgnoreSubtypeNames == true)
            {
                cmdLine.Append(" -ignoreSubtypeNames+");
            }
            else
            {
                cmdLine.Append(" -ignoreSubtypeNames-");
            }

            if (propertiesObject.IgnoreVariableNames == true)
            {
                cmdLine.Append(" -ignoreVariableNames+");
            }
            else
            {
                cmdLine.Append(" -ignoreVariableNames-");
            }

            cmdLine.Append(" -threshold=" + Convert.ToInt32(propertiesObject.ComparisonThreshold));
            cmdLine.Append(" -formatter=xml:\"" + tmpFilename + "\"");
            cmdLine.Append(" -failOnDuplication+");

            if (propertiesObject.IncludeFiles != null)
            {
                
                char [] sep = new char[] { ';', ':', ' ', ',' };

                string[] res = propertiesObject.IncludeFiles.Split(sep);
                foreach (string s in res)
                {
                    cmdLine.Append(" \"" + propertiesObject.SourcePath +
                                    (propertiesObject.SourcePath.EndsWith("\\") ? "**\\" : "\\**\\") + s + "\"");                    
                }
            }
           
            return cmdLine.ToString();
        }
    }
}

